module("move",package.seeall)
map.open("lua\\luamudbot\\mods\\hell\\rooms_all.h") 
on_room=function (name, line, wildcards,styles)
	if ((#styles)==1)and(styles[1]["textcolour"]==ColourNameToRGB("Cyan")) then
		Data["room"]=((styles[1]["text"]))
	end
end
on_exit=function(name, line, wildcards)
  Data["room/exits"]=wildcards[2]
  raise(const.move_event_room_exits)
end

step=function(dir)
  Data["move/step"]=dir
  run(Data["move/step"])
end

stepend=function()
  steptrace(Data["move/step"])
end
steptrace=function(dir)
	if ((dir=="")or(dir==nil)or(dir=="l")) then return end
	if (Data["room/id"]) then  Data["room/id"]=getexitroom(Data["room/id"],dir) end
end

getexits=function(exit)
	exits={}
	re = rex.new ("(\[A-Za-z\]+)")
	re:gmatch (exit, function (m, t)
		table.insert(exits,m)
	 end )
	return exits
end


init=function()
  unbind(const.move_event_stepbegin)
  unbind(const.move_event_stepend)
  bind(const.move_event_room_exits,movestep)
end

movestep=function()
  if Data["room/id"] then
    
  end
  raise(const.move_event_stepend)
end

getexitroom=function (room,dir)

	local exits={}
	local i=0
	local tdir=""
	local texit=""
	if room<0 then return -1 end
	if dir==nil then return room end
	exits=map.getexits(room)
	while (i<#exits) do
		i=i+1
		tdir=dir
		texit=exits[i][1]
		if #tdir > #texit then
			texit=texit.."*"
		elseif #tdir < #texit then
			tdir=tdir.."*"
		end
		if (tdir==texit) then return exits[i][2] end
	end
	return nil
end

defdisableroomlist={}
getroomexits=function (room,enterable,disableroomlist)
	local exits={}
	local i=0
	local roomexits={}
	if disableroomlist==nil then
		disableroomlist=defdisableroomlist
	end
	if room<0 then return nil end
	exits=map.getexits(room)
	while (i<#exits) do
		i=i+1
		if enterable==true then
			if exits[i][2]==-1 then
				break
			elseif exitback[exits[i][1]]==nil then
				break
			elseif getexitroom(exits[i][2],exitback[exits[i][1]])~=room then
				break
			end
		end
		if disableroomlist[exits[i][2]]==true then break end
		table.insert(roomexits,exits[i][1])
	end
	return roomexits
end

roam=function(maxlevel)
  Data["move/type"]="roam"
  Data["move/roam/data"]={newroom("l")}
  Data["move/roam/maxlevel"]=maxlevel or 1
  
  run(const.move_roam_preper)
  raise(const.move_event_stepbegin)
end
filterexits=function(exits,enter)
  output={}
  for k,v in pairs(getexits(exits)) do
    if (const.move_exitbacks[v]~=nil) and (const.move_exitbacks[const.move_exitbacks[enter]]~=const.move_exitbacks[v]) then
      table.insert(output,v)
    end
  end
  return output
end
newroom=function(enter)
 return {enter=enter,exits=nil}
end
roamstep=function(exits)
  local level=table.getn(Data["move/roam/data"])
  local room=Data["move/roam/data"][level]
  room["exits"]=room["exits"] or filterexits(exits,room["enter"])
  if level< Data["move/roam/maxlevel"] and table.getn(room["exits"])>0 then
    exit=table.remove(room["exits"])
    table.insert(Data["move/roam/data"],newroom(exit))
    run(exit)
  else
    exit=const.move_exitbacks[room.enter]
    table.remove(Data["move/roam/data"])
    run(exit)
  end
end

finish=function()
  run("set brief 3")
end

path=function(pathstr)
end
